package c.x.jy.wexin.controller;

import javax.servlet.http.HttpServletRequest;

import c.x.jy.common.annotation.Login;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import c.x.jy.common.utils.R;
import c.x.jy.service.config.WechatConfigService;
import c.x.jy.service.utils.wechat.WechatCommonUtil;
import c.x.jy.service.utils.wechat.WechatConfig;
import jodd.net.URLDecoder;
import lombok.extern.slf4j.Slf4j;

 
@Slf4j
@RestController
@RequestMapping("/wechat")
public class WechatController {

	@Autowired
	private WechatConfigService wechatConfigService;
	/**
	 * 配置调取扫一扫接口
	 * 
	 * @param request
	 * @return
	 * 
	 * @throws Exception
	 */
    @Login
	@ResponseBody
	@RequestMapping("scanConfig")
	public R getWeiXinConfig(HttpServletRequest request, String pageName) throws Exception {
		//WechatDataEntity wechatDataEntity = wechatService.getWXDataInfo();
	 System.out.println(pageName);
		WechatConfig c = wechatConfigService.getDefultConfing();
		WechatConfig wxc =new WechatConfig();
		wxc.setAppId(c.getGzhAppId()); 
		wxc.setJsapiTicket(wechatConfigService.getJsapiTicket(wechatConfigService.getAccessToken(c.getGzhAppId(), c.getGzhAppSecret())));
		wxc.setNonceStr(WechatCommonUtil.createNonceStr(30));
		wxc.setTimestamp(System.currentTimeMillis()+"");

		 
		  if("1".equals(  pageName )){
		  wxc.setUrl(request.getRequestURL().toString() + "?" +  request.getQueryString()); 
		  }else if(StringUtils.isEmpty( pageName )){
			  
		   wxc.setUrl(request.getHeader("referer"));
		   }else{
		  wxc.setUrl(URLDecoder.decode( pageName, "utf-8")); 
		  }
		 
		//wxc.setUrl(pageName);

		WechatCommonUtil.getSignature(wxc);
        
		return R.put(wxc);
	}

}
